<?php

namespace App\Service\GuildManage;

use App\Constants\GuildCode;
use App\Exception\ApiException;
use App\Model\GuildManage\GuildAccount;
use App\Model\GuildManage\GuildBalance;
use App\Service\BaseService;
use App\Service\ServiceTrait\GuildTrait;
use Hyperf\Di\Annotation\Inject;
class InfoService extends BaseService
{
    use \Hyperf\Di\Aop\ProxyTrait;
    use \Hyperf\Di\Aop\PropertyHandlerTrait;
    function __construct(\Hyperf\AsyncQueue\Driver\DriverFactory $driverFactory)
    {
        if (method_exists(parent::class, '__construct')) {
            parent::__construct(...func_get_args());
        }
        $this->__handlePropertyHandler(__CLASS__);
    }
    use GuildTrait;
    /**
     * 用户id
     * @var
     */
    private $actionUserId;
    /**
     * 返回值
     * @var
     */
    protected $return;
    /**
     * @Inject
     * @var GuildBalance
     */
    protected $guildBalanceModel;
    /**
     * @Inject
     * @var GuildAccount
     */
    protected $guildAccountModel;
    public function handle()
    {
        $this->actionUserId = $this->getUserId();
        if (!$this->checkGuildStatus($this->actionUserId)) {
            throw new ApiException('亲,暂时没有此操作权限哦!');
        }
        $this->resetReturnData();
        $this->getDefaultAccount();
        $this->getBalance();
        return $this->return;
    }
    /**
     * 重置返回
     * @return void
     */
    private function resetReturnData()
    {
        $this->return = ['account_id' => 0, 'account_name' => '', 'account' => '', 'way' => [['text' => '当日到账', 'value' => 1, 'service_charge' => bcmul(GuildCode::DAY_SERVICE_CHARGE, 100, 2)], ['text' => '常规到账(下周三到账)', 'value' => 2, 'service_charge' => bcmul(GuildCode::ROUTINE_SERVICE_CHARGE, 100, 2)]], 'have_access_balance' => '0.00', 'agreement' => [['title' => '阅读并同意', 'text' => '《云账户共享经济协议》', 'url' => 'http://web.xtiaowa.com/protocol_yzh.html']]];
    }
    /**
     * 获取默认账号
     * @return void
     */
    private function getDefaultAccount()
    {
        $accountInfo = $this->guildAccountModel->getDefaultAccountByUserId($this->actionUserId);
        if (empty($accountInfo)) {
            $accountInfo = $this->guildAccountModel->where('user_id', $this->actionUserId)->first();
            if (empty($accountInfo)) {
                throw new ApiException('请先添加一个提现方式');
            }
        }
        $this->return['account_id'] = $accountInfo->id;
        $this->return['account_name'] = $accountInfo->type == 1 ? '中国银联' : '支付宝';
        $this->return['account'] = $accountInfo->account;
    }
    /**
     * 获取余额
     * @return void
     */
    private function getBalance()
    {
        $balance = $this->guildBalanceModel->getBalanceInfoByUserId($this->actionUserId);
        if (!empty($balance)) {
            $this->return['have_access_balance'] = $balance->have_access_balance;
        }
    }
}